﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WTable
{
    public class Table : StackPanel
    {
        public Table()
        {
            //Orientation = Orientation.Horizontal;
            // HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Top;
        }
        double penThess = 1;

        protected override void OnRender(DrawingContext dc)
        {
            Pen pen = new Pen(Brushes.Black, penThess);
            double p = penThess / 2d;
            base.OnRender(dc);

            dc.DrawLine(pen, new Point(0, p), new Point(RenderSize.Width, p));

            dc.DrawLine(pen, new Point(p, p), new Point(p, RenderSize.Height));
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            return base.ArrangeOverride(arrangeSize);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Size size = new Size(constraint.Width, 0);
            var childs = InternalChildren.OfType<Row>();
            var childMar = new Thickness(penThess, 0, 0, 0);
            foreach (var child in childs)
            {
                if (childs.ToList().IndexOf(child) == 0)
                    child.Margin = new Thickness(penThess, penThess, 0, 0);
                else
                    child.Margin = childMar;
                child.Measure(constraint);
                size.Height += child.DesiredSize.Height;
            }
            return size;// base.MeasureOverride(constraint);
        }



    }
}
